package mainWindow;

import java.util.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import DataBase.*;
import edu.mit.jwi.item.POS;

public class GuidePanel extends JPanel implements ChangeStyle
{
	private HashSet<String> wordSet;
	private CardLayout cardLayout;
	public void init()
	{
		wordSet = new HashSet<String>();
		cardLayout = new CardLayout();
		this.setLayout(cardLayout);
		setBackground(null);
		setOpaque(false);
	}

	public void changeWord(WordShowPanel wordShowPanel)
	{
		this.setEnabled(true);
		String wordString = wordShowPanel.getWord().getWord();
		if (wordSet.contains(wordString))
		{
			cardLayout.show(this, wordString);
		}
		else
		{
			this.add(wordString, new GuideSheet(wordShowPanel));
			cardLayout.show(this, wordString);
			wordSet.add(wordString);
		}
		showGuide();
	}
	public void removeWord(String wordString)
	{
		wordSet.remove(wordString);
	}
	public void showGuide()
	{
		((LeftPanel)getParent()).setPanel(LeftPanel.GUIDE);
	}
	public void showIndex()
	{
		((LeftPanel)getParent()).setPanel(LeftPanel.INDEX);
	}

	private Image backgroundImage;
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(backgroundImage, 0, 0,this.getWidth(),this.getHeight(),null);
	}
	public void changeStyle(int styleIdx) {
		backgroundImage = StyleTool.getLeftImage(styleIdx);
		this.repaint();		
	}
	

}

class GuideSheet extends JPanel implements ChangeStyle
{
	private JLabel wordLabel;
	private JButton nounButton, verbButton, adjButton, advButton;
	private WordShowPanel wordShowPanel;
	public GuideSheet(WordShowPanel wordShowPanel) {
		this.init(wordShowPanel);
	}
	void init(WordShowPanel wordShowPanel)
	{
		this.wordShowPanel = wordShowPanel;
		Word word = wordShowPanel.getWord();
		wordLabel = new JLabel(word.getWord());
		wordLabel.setFont(new Font("Default",Font.BOLD,20));
		
		setBackground(null);
		setOpaque(false);
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.add(wordLabel);
		
		if (word.exist())
		{
			Dimension buttonDimension = new Dimension(500, 40);
			if (word.hasNounSense())
			{
				nounButton = new JButton("Noun");
				nounButton.setBackground(null);
				nounButton.setOpaque(false);
				nounButton.setContentAreaFilled(false);
				nounButton.setMaximumSize(buttonDimension);
				this.add(nounButton);
				nounButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e) {
						choosePos(POS.NOUN);	
					}			
				});
				nounButton.setEnabled(true);
			}
			if (word.hasVerbSense())
			{
				verbButton = new JButton("Verb");
				verbButton.setBackground(null);
				verbButton.setOpaque(false);
				verbButton.setContentAreaFilled(false);
				verbButton.setMaximumSize(buttonDimension);
				this.add(verbButton);
				verbButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e) {
						choosePos(POS.VERB);	
					}			
				});
				verbButton.setEnabled(true);
			}
			if (word.hasAdjSense())
			{
				adjButton = new JButton("Adjective");
				adjButton.setBackground(null);
				adjButton.setOpaque(false);
				adjButton.setContentAreaFilled(false);
				adjButton.setMaximumSize(buttonDimension);
				this.add(adjButton);
				adjButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e) {
						choosePos(POS.ADJECTIVE);	
					}			
				});
				adjButton.setEnabled(true);
			}
			if (word.hasAdvSense())
			{
				advButton = new JButton("Adverb");
				advButton.setBackground(null);
				advButton.setOpaque(false);
				advButton.setContentAreaFilled(false);
				advButton.setMaximumSize(buttonDimension);
				this.add(advButton);
				advButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e) {
						choosePos(POS.ADVERB);	
					}			
				});
				advButton.setEnabled(true);
			}
		}
		else 
		{
			this.add(new JLabel("NOT FOUND"));
		}
		
		if (word.hasRootForm())
		{
			this.add(Box.createVerticalGlue());
			Dimension buttonDimension = new Dimension(400, 40);
			JLabel tLabel = new JLabel("Do you mean:");
			tLabel.setFont(new Font("Default", Font.ITALIC, 16));
			this.add(tLabel);
			
			ArrayList<String> seeList;
			seeList = word.getRootForm(POS.NOUN);
			if (seeList.size() != 0)
			{
				this.add(new JLabel("Noun:"));
				for (int i = 0; i < seeList.size(); i++)
				{
					final String wordString = seeList.get(i);
					JButton tButton = new JButton(wordString);
					tButton.setBackground(null);
					tButton.setOpaque(false);
					tButton.setContentAreaFilled(false);
					tButton.setMaximumSize(buttonDimension);
					tButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							 Container container = getTopLevelAncestor();
							 if (container instanceof MainFrame)
							 {
								 ((MainFrame)container).addWord(wordString);
							 }
						}				
					});
					this.add(tButton);
				}
			}
			seeList = word.getRootForm(POS.VERB);
			if (seeList.size() != 0)
			{
				this.add(new JLabel("Verb:"));
				for (int i = 0; i < seeList.size(); i++)
				{
					final String wordString = seeList.get(i);
					JButton tButton = new JButton(wordString);
					tButton.setBackground(null);
					tButton.setOpaque(false);
					tButton.setContentAreaFilled(false);
					tButton.setMaximumSize(buttonDimension);
					tButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							 Container container = getTopLevelAncestor();
							 if (container instanceof MainFrame)
							 {
								 ((MainFrame)container).addWord(wordString);
							 }
						}				
					});
					this.add(tButton);
				}
			}
			seeList = word.getRootForm(POS.ADJECTIVE);
			if (seeList.size() != 0)
			{
				this.add(new JLabel("Adj:"));
				for (int i = 0; i < seeList.size(); i++)
				{
					final String wordString = seeList.get(i);
					JButton tButton = new JButton(wordString);
					tButton.setBackground(null);
					tButton.setOpaque(false);
					tButton.setContentAreaFilled(false);
					tButton.setMaximumSize(buttonDimension);
					tButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							 Container container = getTopLevelAncestor();
							 if (container instanceof MainFrame)
							 {
								 ((MainFrame)container).addWord(wordString);
							 }
						}				
					});
					this.add(tButton);
				}
			}
			seeList = word.getRootForm(POS.ADVERB);
			if (seeList.size() != 0)
			{
				this.add(new JLabel("Adv:"));
				for (int i = 0; i < seeList.size(); i++)
				{
					final String wordString = seeList.get(i);
					JButton tButton = new JButton(wordString);
					tButton.setBackground(null);
					tButton.setOpaque(false);
					tButton.setContentAreaFilled(false);
					tButton.setMaximumSize(buttonDimension);
					tButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							 Container container = getTopLevelAncestor();
							 if (container instanceof MainFrame)
							 {
								 ((MainFrame)container).addWord(wordString);
							 }
						}				
					});
					this.add(tButton);
				}
			}
		}		
	}
	private void choosePos(POS pos)
	{
		wordShowPanel.showPos(pos);
	}
	@Override
	public void changeStyle(int styleIdx) {
		// TODO Auto-generated method stub
		
	}
}